// run: $exec < input
#include <iostream>
#include <algorithm>

int const inf = 1000000000;
int const maxn = 507;

int map[maxn][maxn];
int vis[maxn][maxn];
int slack[maxn];
int linked[maxn];
int label_x[maxn], label_y[maxn];
bool visited_x[maxn], visited_y[maxn];
int nx, ny;

void init_label()
{
	std::fill(label_x, label_x + nx, -inf);
	std::fill(label_y, label_y + ny, 0);
	for (int i = 0; i < nx; i++)
		for (int j = 0; j < ny; j++)
			if (map[i][j] > label_x[i]) label_x[i] = map[i][j];
}

bool hungarian_dfs(int u)
{
	visited_x[u] = true;
	for (int i = 0; i < ny; i++) {
		if (map[u][i] <= -inf) continue;
		if (visited_y[i]) continue;
		int tmp = label_x[u] + label_y[i] - map[u][i];
		if (tmp == 0) {
			visited_y[i] = true;
			if (linked[i] == -1 || hungarian_dfs(linked[i])) {
				linked[i] = u;
				return true;
			}
		} else
			if (slack[i] > tmp) slack[i] = tmp;
	}
	return false;
}

int kuhn_munkres()
{
	init_label();
	std::fill(linked, linked + ny, -1);
	for (int i = 0; i < nx; i++) {
		std::fill(slack, slack + ny, inf);
		while (true) {
			std::fill(visited_x, visited_x + nx, false);
			std::fill(visited_y, visited_y + ny, false);
			if (hungarian_dfs(i)) break;
			int d = inf;
			for (int i = 0; i < ny; i++)
				if (!visited_y[i] && d > slack[i]) d = slack[i];
			for (int i = 0; i < nx; i++)
				if (visited_x[i]) label_x[i] -= d;
			for (int i = 0; i < ny; i++)
				if (visited_y[i]) label_y[i] += d;
				else				slack[i] -= d;
		}
	}
	int max_value = 0;
	for (int i = 0; i < ny; i++)
		if (linked[i] != -1) max_value += map[linked[i]][i];
	return max_value;
}

bool girl[maxn];
bool first[maxn];
int n;

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::cin >> n;
		for (int i = 0; i < n; i++) {
			char ch; std::cin >> ch;
			first[i] = ch == '0';
		}
		for (int i = 0; i < n; i++) {
			char ch; std::cin >> ch;
			girl[i] = ch == '0';
		}
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				vis[i][j] = map[i][j] = 0;
		nx = ny = n;
		for (int i = 0; i < n; i++) {
			int m; std::cin >> m;
			for (int j = 0, x; j < m; j++) {
				std::cin >> x;
				vis[i][x - 1] = vis[x - 1][i] = 1;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (!vis[i][j] && !vis[j][i] && (first[i] ^ first[j])) {
					int ret = 20000;
					ret += girl[i];
					ret += girl[j];
					if (first[i] && !first[j]) map[i][j] = ret;
					else if (!first[i] && first[j]) map[j][i] = ret;
				}
			}
		}
		nx = ny = n;
		int tmp = kuhn_munkres();
		std::cout << tmp / 20000 << ' ' << tmp % 20000 << '\n';
		for (int i = 0; i < n; i++)
			if (linked[i] != -1 && map[linked[i]][i])
				std::cout << linked[i] + 1 << ' ' << i + 1 << '\n';
	}
}

